<?php
App::uses('AppController', 'Controller');
/**
 * Pagos Controller
 *
 * @property Pago $Pago
 */
class PagosController extends AppController {
 
    public $layout = 'colegio';
    public $helpers = array('Utilidades');
    public $components=array('FechasConvert');
    //public $components = array('Fechasconvert');

    public function beforeFilter()
    {
        parent::beforeFilter();
        $this->Auth->allow('*');
    }
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Pago->recursive = 0;
		$this->set('pagos', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		
        $this->Pago->id = $id;
		if (!$this->Pago->exists()) {
			throw new NotFoundException(__('Pago invalido'));
		}
        $pago = $this->Pago->read(null, $id);

		$this->set(compact('pago'));
	}

public function anadir($id=null) {
	   $this->layout='ajax';
		if (!empty($this->request->data)) {
		  
            $fecha = $this->FechasConvert->doFechaPago($this->request->data['Pago']['fechapago']);
            
			$this->request->data['Pago']['fechapago']= $fecha;
            
            $this->Pago->create();
			if ($this->Pago->save($this->request->data)) {
				$this->Session->setFlash(__('El pago ha sido guardado'), 'msgbueno');
				$this->redirect(array('controller'=>'planespagos', 'action' => 'view', $id));
			} else {
				$this->Session->setFlash(__('El pago no pudo ser guardado. Por favor, trate nuevamente.'), 'msgerror');
			}
		}
		$planespagos = $this->Pago->Planespago->find('list', array('fields'=>array('Planespago.id', 'Planespago.nombre')));
		$this->set(compact('planespagos', 'id'));
	}
/**
 * add method
 *
 * @return void
 */
	public function add() {
	   
		if ($this->request->is('post')) {
		  
            $fecha = $this->FechasConvert->doFechaPago($this->request->data['Pago']['fechapago']);
            
			$this->request->data['Pago']['fechapago']= $fecha;
            
            $this->Pago->create();
			if ($this->Pago->save($this->request->data)) {
				$this->Session->setFlash(__('El pago ha sido guardado'), 'msgbueno');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El pago no pudo ser guardado. Por favor, trate nuevamente.'), 'msgerror');
			}
		}
		$planespagos = $this->Pago->Planespago->find('list', array('fields'=>array('Planespago.id', 'Planespago.nombre')));
		$this->set(compact('planespagos'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */

 public function ajaxedit($id=null, $idPlan=null){
      $this->layout = 'ajax';  
      $this->Pago->id = $id;
		if (!$this->Pago->exists()) {
			throw new NotFoundException(__('Pago invalido'));
            $this->redirect(array('action' => 'view', $idPlan));
		}
		if (!empty($this->request->data)) {
            $fecha = $this->FechasConvert->doFechaPago($this->request->data['Pago']['fechapago']);
			$this->request->data['Pago']['fechapago']= $fecha;
            //debug($this->request->data);exit;
			if ($this->Pago->save($this->request->data)) {
				$this->Session->setFlash(__('El pago ha sido guardado'), 'msgbueno');
				$this->redirect(array('controller'=>'planespagos', 'action' => 'view', $idPlan));
			} else {
				$this->Session->setFlash(__('El pago no pudo ser guardado. Por favor, trate nuevamente.'), 'msgerror');
			}
		} else {
			$this->request->data = $this->Pago->read(null, $id);
            $this->request->data['Pago']['fechapago'] = $this->FechasConvert->doRevertFechaPago($this->request->data['Pago']['fechapago']);
           	$planespagos = $this->Pago->Planespago->find('list', array('fields'=>array('Planespago.id', 'Planespago.nombre')));
		   $this->set(compact('planespagos', 'idPlan'));
		}
	
 }
	public function edit($id = null) {
	   
		$this->Pago->id = $id;
		if (!$this->Pago->exists()) {
			throw new NotFoundException(__('Pago invalido'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
            $fecha = $this->FechasConvert->doFechaPago($this->request->data['Pago']['fechapago']);
			$this->request->data['Pago']['fechapago']= $fecha;
            //debug($this->request->data);exit;
			if ($this->Pago->save($this->request->data)) {
				$this->Session->setFlash(__('El pago ha sido guardado'), 'msgbueno');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El pago no pudo ser guardado. Por favor, trate nuevamente.'), 'msgerror');
			}
		} else {
			$this->request->data = $this->Pago->read(null, $id);
            $this->request->data['Pago']['fechapago'] = $this->FechasConvert->doRevertFechaPago($this->request->data['Pago']['fechapago']);
		}
		$planespagos = $this->Pago->Planespago->find('list', array('fields'=>array('Planespago.id', 'Planespago.nombre')));
		$this->set(compact('planespagos'));
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Pago->id = $id;
		if (!$this->Pago->exists()) {
			throw new NotFoundException(__('Pago invalido'));
		}
		if ($this->Pago->delete()) {
			$this->Session->setFlash(__('Pago eliminado'), 'msgbueno');
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('El Pago no ha sido eliminado'), 'msgerror');
		$this->redirect(array('action' => 'index'));
	}
    public function eliminar($id = null, $idPlan=null) {
	
		$this->Pago->id = $id;
		if (!$this->Pago->exists()) {
			throw new NotFoundException(__('Pago invalido'));
            $this->redirect(array('controller'=>'planespagos', 'action' => 'view', $idPlan));
		}
        $pagosalumnos=$this->AlumnosPago->find('count',array('conditions'=>array('AlumnosPago.pago_id'=>$id)));
            if($pagosalumnos!=0){
                $this->Session->setFlash(__('El Pago no puede ser eliminado por existir pagos a este item'), 'msgalert');
			    $this->redirect(array('controller'=>'planespagos', 'action' => 'view', $idPlan));
            }
		if ($this->Pago->delete()) {
			$this->Session->setFlash(__('Pago eliminado'), 'msgbueno');
			$this->redirect(array('controller'=>'planespagos', 'action' => 'view', $idPlan));
		}
		$this->Session->setFlash(__('El Pago no ha sido eliminado'), 'msgerror');
		$this->redirect(array('controller'=>'planespagos', 'action' => 'view', $idPlan));
	}
}
